// pages/user/index.js
import Router from '../../utils/router.js';
import base from '../../config/base.js';
import authorize from '../../utils/authorize.js'
import util from '../../utils/util.js'
import myindexService from '../../services/myindex.js'
import service_money from '../../services/money.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    fshow: 0,
    redpag: {
      show:'',
      types: 1,
      animate: 0,
      redpagflag: false
    },
    encode: encodeURIComponent('/pages/register/open?signupdate=signupdate'),
    version: '',
    icon_right: base.static + '/my/arrow.png',
    address: base.static + '/my/wodedizhi.png',
    linqian: base.static + '/my/linqian.png',
    beike: base.static + '/my/beike.png', 
    mask_conchimg: base.static + '/my/mask_conch.png',
    dianpuyouhuiquan: base.static + '/my/dianpuyouhuiquan@2x.png',
    shopkeeper: base.static + '/my/shopkeeper.png',
    addressNum: 0,
    kefu: base.static + '/my/lianxikefu.png',
    user_info:{},
    //订单列表 状态
    orderstatuslist: [
      {
        orderImg: base.static + '/my/daifukuan.png',
        ordertext: '待付款',
        orderstatus: 3009,
        xianFlag: true,
        wait_num: 0,
        width: 34,
        right: 18
      },
      // {
      //   orderImg: base.static + '/my/yifukuan.png',
      //   ordertext: '已付款',
      //   orderstatus: 3010,
      //   xianFlag: true
      // },
      {
        orderImg: base.static + '/my/daifahuo.png',
        ordertext: '待发货',
        orderstatus: 3010,
        xianFlag: true,
        wait_num: 0,
        width: 48,
        right: 31
      },
      {
        orderImg: base.static +'/my/daishouhuo.png',
        ordertext: '待收货',
        orderstatus: 3003,
        xianFlag: true,
        wait_num: 0,
        width: 58,
        right: 41
      },
      {
        orderImg: base.static +'/my/shouhou.png',
        ordertext: '售后',
        orderstatus: 3007,
        xianFlag: false
      }
    ],
    //个人信息 功能
    getaddressflag: true,
    conchnum: '0',
    coupon_num: 0,
    num_x: '',
    num_y: '',
    show_data: '',
    balance: '',
    balance: '',
    count_balance: '',
    isShenhe: '',
    transmit:{//formid 
      style:'position:absolute;width:100%;height:100%;background:red;opacity:0;',
      value:''
    }
  },
  gocoupon() {
    Router.go('mycoupon');
  }, 
  /**
   * 订单页面跳转
   */
  goorderlist:function(e){
    var that = this,
      dataset = e.currentTarget.dataset;
    Router.go('orderList', `status=${dataset.status}`);
  },
  /**
   * 获取邻钱
   */
  goconch:function(){
    Router.go('linprice', `conchnum=${this.data.conchnum}`);
  },
  getconchnum: function () {
    let that = this,
      param = {},
      loadflag = true;
    myindexService.getconch(param, res => {
      if (res && res.code == 1) {
        let conchnum = res.data.conch;
        // if (conchnum !== null && conchnum !== undefined && conchnum !== '' && !isNaN(Number(conchnum))) {
        //   conchnum = util.toDecimal2(util.floatAccMul(conchnum, 1));
        // }
        that.setData({
          conchnum
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    },res=>{
      wx.stopPullDownRefresh();
    });
  },
  getordernumber:function(){
    let that = this,
      param = {},
      loadflag = true;
    const width = function(num){
      let a = 34;
      if (num){
        if (num > 0 && num< 10) {
          a = 34;
        } else if (num > 10 && num < 99) {
          a = 48;
        } else if (num > 99) {
          a = 58;
        }
      }
      return a;
    };
    myindexService.getordernumber(param, res => {
      let orderstatuslist = that.data.orderstatuslist;
      // res.wait_pay += 5666;
      // res.wait_send += 55;
      // res.wait_get += 555;
      let wait_pay = res.wait_pay > 99 ? '99+' : res.wait_pay
      let wait_send = res.wait_send > 99 ? '99+' : res.wait_send
      let wait_get = res.wait_get > 99 ? '99+' : res.wait_get
      for (let i in orderstatuslist){
        let model = orderstatuslist[i];
        if (model.orderstatus == 3009) {
          model.wait_num = wait_pay;
          model.width = width(res.wait_pay);
          model.right = model.width - 17;
        }
        if (model.orderstatus == 3010) {
          model.wait_num = wait_send;
          model.width = width(res.wait_send);
          model.right = model.width - 17;
        }
        if (model.orderstatus == 3003) {
          model.wait_num = wait_get;
          model.width = width(res.wait_get);
          model.right = model.width - 17;
        }
      }
      that.setData({
        orderstatuslist
      });
    }, res => {
      that.getconchnum();
    });
  },
  /**
   * 查看微信收货地址
   */
  gettomyaddress: function () {
    let that = this,
      addressNum = that.data.addressNum,
      getaddressflag = that.data.getaddressflag;
    if (getaddressflag) {
      getaddressflag = false;
      that.setData({
        getaddressflag
      });
      authorize('address', (b) => {
        addressNum++;
        that.setData({
          addressNum
        });
        if (b) {
            wx.chooseAddress({
              success: (res) => { },
              complete: (res) =>{ }
            })
        } else {
          // 弹出一个窗口 引导打开
          if (addressNum>1){
            wx.showModal({
              title: '授权提示',
              content: '您未授权通讯地址，设置后正常使用',
              showCancel: false,
              success: res => {
                wx.openSetting({
                  success: res => { }
                })
              }
            })
          }
        }
        setTimeout(() => {
          getaddressflag = true;
          that.setData({
            getaddressflag
          });
        }, 500)
      })
    }
  },
  //去提现明细页面
  moneydetailhandle() {
    Router.go('muchdetail');

  },
  //去提现页面
  putforward: function () {
    if (this.data.balance > 0){
      if (this.data.is_withdarw != 1){
        wx.showToast({
          title: '2月1日-2月15日春节放假期间暂停提现',
          icon: 'none'
        })
        return;
      }
      Router.go('putforward', `balance=${this.data.balance}&count_balance=${this.data.count_balance}`); 
    } 
  },
  goshopkeeper(){
    Router.go('shopkeeper')
  },
  /**
   * 获取优惠券数量
   */
  getcouponnum(){
    myindexService.getcouponnum({}, res=>{
      if(res && res.code==1){
        this.setData({
          coupon_num: res.data.not_use
        });
      }
    })
  },
  //获取用户余额
  getCustomerBalance(){
    let that = this;
    service_money.getcustomerbalance({},function(res){
      if(res && res.code == 1){
         that.setData({
           balance: res.balance,
           count_balance: res.count,
           is_withdarw: res.is_withdarw
         })
       }    
    })
  },
  //检查是否要显示开店通道
  getshowcheck(){
    let that = this;
    myindexService.showcheck({},function(res){
      if (res && res.code == 1) {
        if(res.data){
          that.setData({
            num_x: res.data.x,
            num_y: res.data.y
          })
        }
        that.setData({
          show_data: res.data,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let redpag = this.data.redpag;
    let user_info = wx.getStorageSync('user_info');
    let activity = wx.getStorageSync('activity');
    let isShenhe = wx.getStorageSync('isShenhe');
    let redpag_show = activity.prize_activity;
    redpag.show = redpag_show;
    this.setData({
      user_info: user_info,
      redpag: redpag,
      version: getApp().globalData.version,
      isShenhe: isShenhe,
      redpag: redpag,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getordernumber();
    this.getcouponnum();
    this.getCustomerBalance()    
    this.getshowcheck()//检查是否要显示开店通道    
    if (this.data.user_info && this.data.user_info.info_authorize) {
     
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  onPageScroll(e) {
    util.scrollhongbao(this);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getordernumber();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
})